package com.hbt.yiqing.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.hbt.yiqing.R;
import com.hbt.yiqing.db.UserTable;
import com.hbt.yiqing.entity.Users;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.username)
    EditText username;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.info)
    EditText info;
    @BindView(R.id.login_btn)
    Button loginBtn;
    @BindView(R.id.apply_btn)
    Button applyBtn;
    @BindView(R.id.allow_btn)
    Button allowBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

    }//onCreate end

    @OnClick(R.id.allow_btn)
    void allow_jump_click(){
        Intent intent = new Intent(LoginActivity.this, AllowActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.apply_btn)
    void applyOnClick(){
        UserTable userTable = new UserTable(this);
        SQLiteDatabase db = userTable.getWritableDatabase();
        ContentValues values = new ContentValues();
        Users user = getUser();

        if (user!=null){
            //查看是否重名
            if(hasTheSameUsername()){
                username.setError("用户名已存在");
                return;
            }
            values.put(UserTable.USERNAME, user.getUsername());
            values.put(UserTable.PASSWORD, user.getPassword());
            values.put(UserTable.INFO, user.getInfo());
            values.put(UserTable.ALLOW, user.getAllow());
            db.insert(UserTable.TABLE_NAME,UserTable.INFO,values);

            AlertDialog dialog = new AlertDialog.Builder(this).create();
            dialog.setTitle("提示信息");
            dialog.setMessage("已申请，等待审核");
            dialog.show();

            username.setText("");
            password.setText("");
            info.setText("");

        }else{
            AlertDialog dialog = new AlertDialog.Builder(this).create();
            dialog.setTitle("提示信息");
            dialog.setMessage("请输入完整信息");
            dialog.show();
        }

    }//applyOnClick end

    @OnClick(R.id.login_btn)
    void loginOnClick(){
        UserTable userTable = new UserTable(this);
        SQLiteDatabase db = userTable.getWritableDatabase();
        ContentValues values = new ContentValues();
        Users user = getUser();
        if (user!=null){
            values.put(UserTable.USERNAME, user.getUsername());
            values.put(UserTable.PASSWORD, user.getPassword());
            values.put(UserTable.INFO, user.getInfo());
            values.put(UserTable.ALLOW, user.getAllow());
//            db.insert(UserTable.TABLE_NAME,UserTable.INFO,values);
            Cursor c = db.query(UserTable.TABLE_NAME,null,UserTable.USERNAME+"=? and "+UserTable.PASSWORD+"=? and "+UserTable.ALLOW+"=?",new String[]{user.getUsername(), user.getPassword(), "1"},null,null,null);

            if(c.getCount()>0){
                //跳转页面
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
            }else{
                AlertDialog dialog = new AlertDialog.Builder(this).create();
                dialog.setTitle("提示信息");
                dialog.setMessage("请先申请");
                dialog.show();
            }

        }else{
            AlertDialog dialog = new AlertDialog.Builder(this).create();
            dialog.setTitle("提示信息");
            dialog.setMessage("请检查输入");
            dialog.show();
        }
    }


    boolean hasTheSameUsername(){
        Users user = getUser();
        UserTable userTable = new UserTable(this);
        SQLiteDatabase db = userTable.getWritableDatabase();
        Cursor c = db.query(UserTable.TABLE_NAME,null,UserTable.USERNAME+"=?",new String[]{user.getUsername()},null,null,null);
        if(c.getCount() > 0){
            return true;
        }
        return false;
    }


    Users getUser(){
        String username_str = username.getText().toString();
        String password_str = password.getText().toString();
        String info_str = info.getText().toString();
        Users user = new Users();
        if(username_str.equals("")||password_str.equals("")){
            return null;
        }
        user.setUsername(username_str);
        user.setPassword(password_str);
        user.setInfo(info_str);
        user.setAllow("0");
        return user;
    }

}//class end

